package cn.com.dhc.danlu.widget.couponview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.widget.base.BasePopupWindow;

/**
 * 领券中心--筛选弹窗页面
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * User: Liu liqi(liuliqi@danlu.com)
 * Date: 2016-09-22
 * Time: 16:50
 * Description: .....
 */
public class CouponFilterPopupView extends BasePopupWindow {
    private EditText etDealerName;  // 发放店铺输入框
    private EditText etGoodsName; // 适用商品输入框
    private Button btnReset;
    private Button btnSure;
    private List<Button> btnWines;

    private int[] btnWineIds = {
            R.id.btn_liquor, R.id.btn_claret, R.id.btn_tea, R.id.btn_foreign, R.id.btn_beer, R.id.btn_drinks, R.id.btn_others
    };
    private String[] mTags = new String[]{
            CommonConst.IntentKey.WINE_CODE, CommonConst.IntentKey.RED_WINE_CODE, CommonConst.IntentKey.TEA_CODE, CommonConst.IntentKey.FOREIGN_CODE, CommonConst.IntentKey.BEER_CODE, CommonConst.IntentKey.DRINKS_CODE, CommonConst.IntentKey.FILTER_OTHERS_CODE
    };
    private ISureClickListener mListener;

    public CouponFilterPopupView(Context context, ViewGroup rootView) {
        super(context);

        init(rootView);
    }

    private void init(ViewGroup rootView) {
        View popView = LayoutInflater.from(rootView.getContext()).inflate(R.layout.popview_coupon_wine_filter, rootView, false);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        setAnimationStyle(R.style.coupon_popup_wine_filter);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        initChildView(popView);
        setChileViews(popView);
        setContentView(popView);
    }

    private void initChildView(View popView) {
        btnWines = new ArrayList<>(7);
        for (int i = 0; i < btnWineIds.length; i++) {
            btnWines.add((Button) popView.findViewById(btnWineIds[i]));
            btnWines.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clearOtherWinesState(v.getId(), false);
                }
            });
        }
        btnSure = (Button) popView.findViewById(R.id.btn_sure);
        btnReset = (Button) popView.findViewById(R.id.btn_reset);
        etGoodsName = (EditText) popView.findViewById(R.id.et_goods_name);
        etDealerName = (EditText) popView.findViewById(R.id.et_dealer_name);
    }

    private void setChileViews(View popView) {
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.onDataCallback(getCurrentSelectType(), etDealerName.getText().toString().replace (" ",""), etGoodsName.getText().toString().replace (" ",""));
                }
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearAllState();
            }
        });
        popView.findViewById(R.id.pop_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isShowing()) {
                    dismiss();
                }
            }
        });
    }

    /**
     * 清除其他按钮的选中状态
     *
     * @param btnWineId
     *         当前选中的按钮的ID
     * @param isClearAll
     *         是否清除所有
     */
    private void clearOtherWinesState(int btnWineId, boolean isClearAll) {
        for (Button btnWine : btnWines) {
            if (btnWine.isSelected()) {
                btnWine.setSelected(false);
            }
            // 当点击按钮id与传入按钮id相等，且不清除所有选中时，保留已选中状态
            if (btnWine.getId() == btnWineId && !isClearAll) {
                btnWine.setSelected(true);
            }
        }
    }

    /**
     * 获取商品分类码
     *
     * @return 商品分类码
     */
    private String getCurrentSelectType() {
        for (int i = 0; i < btnWines.size(); i++) {
            boolean selected = btnWines.get(i).isSelected();
            if (selected) {
                return mTags[i];
            }
        }
        return "";
    }

    /**
     * 清理掉所有数据
     */
    public void clearAllState() {
        etGoodsName.setText(null);
        etDealerName.setText(null);
        clearOtherWinesState(btnWineIds[0], true);
    }

    public void setSureClickListener(ISureClickListener mListener) {
        this.mListener = mListener;
    }

    public interface ISureClickListener {
        void onDataCallback(String categoryCode, String dealerName, String goodsName);
    }
}